/**
 * Table List Operate Module.
 */
define(['jquery'], function($) {
	var ListAsyncOp;
	ListAsyncOp = (function () {
		function ListAsyncOp () {
			this.defaults = {
				url: '',
				data: {
					rnd: (new Date()).getTime()
				},
				before: function() {},
				success: function () {},
				always: function () {}
			};
			this.settings = {};
		}

		ListAsyncOp.prototype.init = function (options) {
			$.extend(true, this.settings, this.defaults, options);
		};
		ListAsyncOp.prototype.operate = function () {
			var targetUrl = this.settings.url,
				opData = this.settings.data,
				doneCb = this.settings.success,
				alwaysCb = this.settings.always,
				beforeCb = this.settings.before;
			$.ajax({
				url: targetUrl,
				type: 'JSON',
				type: 'POST',
				data: opData,
				beforeSend: beforeCb
			})
			.done(function (data) {
				doneCb(data);
			})
			.fail(function () {
				console.log("Operate Error!");
			})
			.always(function (data) {
				// Back to root when session expire.
				VAM.backRoot(data);
				alwaysCb();
			});
		};
		ListAsyncOp.prototype.listOp = function (options) {
			this.init(options);
			this.operate();
		};

		return ListAsyncOp;
	})();
	
	return new ListAsyncOp;
});